package net.lzzy.practicesapi1.web;

import lombok.Getter;
import lombok.Setter;
import net.lzzy.practicesapi1.entities.Chapter;
import net.lzzy.practicesapi1.entities.Course;

import java.util.Date;
import java.util.UUID;

/**
 * @author 梁园钲
 */
@Getter
@Setter
public class VmCourse {
    private UUID id;

    private String name;

    private String cover;

    private String desc;

    private Date createTime;

    private Date updateTime;

    private boolean open;

    private UUID teacherId;

    private String teacherName;

    private String teacherAvatar;

    private int countStudents;

    private int countChapters;
    private VmCourse(){
    }
    public static VmCourse create(Course course,boolean teacher){
        VmCourse vm = new VmCourse();
        vm.setId(course.getId());
        vm.setName(course.getName());
        vm.setCover(vm.getCover());
        vm.setDesc(course.getDescription());
        vm.setOpen(course.isOpen());
        vm.setCreateTime(course.getCreateTime());
        vm.setUpdateTime(course.getUpdateTime());
        vm.setTeacherName(course.getTeacher().getNickName());
        vm.setTeacherAvatar(course.getTeacher().getAvatar());
        vm.setCountStudents(course.getChapters().size());
        if (teacher){
            vm.setTeacherId(course.getTeacher().getId());
            vm.setCountChapters(course.getChapters().size());
        }else {
            vm.setCountChapters((int) course.getChapters().stream().filter(Chapter::isOpen).count());
        }
        return vm;
    }

}
